import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:im_app/im/page/view/LoadingView.dart';
import 'package:im_app/im/page/view/MonthView.dart';

import '../../pojo/ImPojo.dart';
import '../../util/ImConstant.dart';
import '../bloc/MessageHistoryDayCubit.dart';

/// 消息历史页面--通过时间定位消息
class MessageHistorySearchDayPage extends StatefulWidget {
  final ImSession session;

  const MessageHistorySearchDayPage({required this.session, super.key});

  @override
  State<StatefulWidget> createState() => _MessageHistoryDaySate();
}

class _MessageHistoryDaySate extends State<MessageHistorySearchDayPage> {

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    var messageHistoryDayCubit = context.watch<MessageHistoryDayCubit>();
    messageHistoryDayCubit.loadData(this.widget.session.id!);
    return Scaffold(
        appBar: _buildAppBar(context),
        body: LoadingView(
          state: messageHistoryDayCubit.state.state,
          child: Column(children: [
            Container(
              padding: const EdgeInsets.only(left: 30, right: 10, top: 5),
              height: 30,
              child: GridView(gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 7, childAspectRatio: 1.5),
                children: const <Widget>[
                  Text("一"),
                  Text("二"),
                  Text("三"),
                  Text("四"),
                  Text("五"),
                  Text("六"),
                  Text("日"),
                ],
              ),
            ),
            Expanded(child: ListView.builder(
              itemBuilder: (context, index) => _buildMonthPane(messageHistoryDayCubit.state.monthList.elementAt(index)),
              itemCount: messageHistoryDayCubit.state.monthList.length,
            ))
          ],),
        )
    );
  }

  AppBar _buildAppBar(BuildContext context) {
    return AppBar(
      backgroundColor: ImConstant.IM_MAIN_BACKGROUND_COLOR,
      leading: GestureDetector(
        child: const Padding(
          padding: EdgeInsets.only(left: 0, right: 0),
          child: Icon(Icons.chevron_left, color: Colors.black),
        ),
        onTap: () => Navigator.of(context).pop(),
      ),
      titleSpacing: 5,
      title: const Text(
        "选定日期",
        style: TextStyle(color: Colors.black),
      ),
    );
  }

  Widget _buildMonthPane(List<DateTime> dates) {
    var month = dates[0];
    return MonthView(session: this.widget.session, month: month, days: dates);
  }
}
